package com.zb.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.system.OmsOperList;
import com.zb.entity.system.OmsOperMerchantBranch;
import com.zb.service.base.OperMerchantBranchService;
import com.zb.util.ActionUtils;

/**
 * 
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/operMerchantBranchApi")
public class OperMerchantBranchAction extends BaseAction
{
	@Autowired
	private OperMerchantBranchService operMerchantBranchServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> add(HttpServletRequest request, HttpServletResponse response)
	{
		return operMerchantBranchServiceImpl.addOmsOperMerchantBranch(this.getObjFromRequest(request,true,false));
	}
	
	@RequestMapping(value="update/{oper_merchant_branch_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateOperMerchant(@PathVariable(value="oper_merchant_branch_id") String oper_merchant_branch_id,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsOperMerchantBranch obj = this.getObjFromRequest(request,false,false);
		obj.setOper_merchant_branch_id(oper_merchant_branch_id);
		return operMerchantBranchServiceImpl.updateOmsOperMerchantBranch(obj);
	}
	
	@RequestMapping(value="delete/{oper_merchant_branch_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(@PathVariable(value="oper_merchant_branch_id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return operMerchantBranchServiceImpl.deleteOmsOperMerchantBranch(id);
	}
	//查询当前登录用户所属实体的运营商分公司列表
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		OmsOperList user = this.getLoginUser(request);
		OmsOperMerchantBranch branch = this.getObjFromRequest(request,false,true);
		if (user.getIsSupper() < 0)
		{
			branch.setOper_merchant_id(user.getEntity_id());
		}
//		return operMerchantBranchServiceImpl.findPageOmsOperMerchantBranch(this.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
		return operMerchantBranchServiceImpl.findPageOmsOperMerchantBranch(this.buildQueryParam(request, true), branch);
	}
	//查询某个实体的运营商分公司列表
	@RequestMapping(value="findListByOperMerchantId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findListByOperMerchantId(HttpServletRequest request, HttpServletResponse response)
	{
		String operMerchantId = request.getParameter("operMerchantId");
		OmsOperMerchantBranch branch = new OmsOperMerchantBranch();
		branch.setOper_merchant_id(operMerchantId);
		return operMerchantBranchServiceImpl.findPageOmsOperMerchantBranch(this.buildQueryParam(request, true), branch);
	}
	@RequestMapping(value="findObj/{oper_merchant_branch_id}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findObj(@PathVariable(value="oper_merchant_branch_id") String id,
			HttpServletRequest request, HttpServletResponse response)
	{
		return operMerchantBranchServiceImpl.findObj(id);
	}
	
	private OmsOperMerchantBranch getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		String loginUserId = this.getLoginUserId(request);
		return ActionUtils.getParamInstance(OmsOperMerchantBranch.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
